﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using eCitizen.eAlerts.Constants;
using eCitizen.DataContracts;
using eCitizen.DataAccess;
using eCitizen.eAlerts.Utilities;

namespace eCitizen.eAlerts.BusinessService
{
    public class SendReportService : ServiceBase,IServiceBase
    {

       public void RunService()
        {
            SftpConnectionManager connectionManager = new SftpConnectionManager();
            FileManager requestFileManager = new FileManager();
            DateTime statusReportDate = DateTime.Now;

            // Start of the SendAlertService
            CurrentServiceHelper.StartBatchJobWatch(ServiceType.SendReportService);
            // Start of the SendAlertService

            foreach (SftpConfiguration config in requestFileManager.SftpConfigurations)
            {
                CurrentConfiguration = config;

                CurrentServiceHelper.StartServiceInstanceWatch(CurrentConfiguration.Agency,CurrentConfiguration.Service);

                StatusReportManager reportManager = new StatusReportManager(statusReportDate);
                SubscriberManager subscriberManager = new SubscriberManager();
                String agencyServiceCode = config.Service; //Service Code from the configuration

                // DataContracts which will hold the status report records
                SmsStatusReport smsStatusReport = new SmsStatusReport();
                EmailStatusReport emailStatusReport = new EmailStatusReport();
                List<Subscriber> subscriberList  = new List<Subscriber>();

                // Get SmsReport files from sFTP OneSms and Upload into eCitizen DB
                #region "Upload SmsReports into DB"

                connectionManager.TransferOneSmsReportFilesToLocal(config);

                List<ServiceRequest> requestfiles = requestFileManager.GetReportFiles(config);
                foreach (ServiceRequest currentRequest in requestfiles)
                {

                    CurrentServiceInfo = currentRequest.CurrentServiceInfo;
                    RequestFileManager requestManager = new RequestFileManager();

                    CurrentServiceHelper.LogeAlertFileToProcess(currentRequest.RequestFileFullPath, false, statusReportDate);
                    
                    bool isParseSuccessful = requestManager.TryParseSendReportRecords(currentRequest, out smsStatusReport);

                    if (isParseSuccessful)
                    {
                        reportManager.LogSmsStatusReport(smsStatusReport);
                    }

                }

                #endregion

                #region "Send SubscriberList Report per Service"

                ServiceRequest requestFile = null;
                subscriberList = subscriberManager.GetSubscriberListByService(agencyServiceCode);
                requestFile = reportManager.SendSubscriptionReport(subscriberList);

                CurrentServiceHelper.LogeAlertFileToProcess(requestFile.RequestFileName, false, statusReportDate);
                CurrentServiceHelper.LogeAlertFileToProcess(requestFile.ChecksumFileName, false, statusReportDate);

                requestFile = reportManager.SendSubscriberProfileReport(subscriberList);

                CurrentServiceHelper.LogeAlertFileToProcess(requestFile.RequestFileName, false, statusReportDate);
                CurrentServiceHelper.LogeAlertFileToProcess(requestFile.ChecksumFileName, false, statusReportDate);

                #endregion

                #region "Send Email Status Report"

                // Provide the necessary information for the ReportManager to get the records from the db

                emailStatusReport.Agency = config.Agency;
                emailStatusReport.ServiceId = config.Service;
                emailStatusReport.ReportDate = statusReportDate;

                // Get the status records from the db
                emailStatusReport = reportManager.GetEmailStatusReport(emailStatusReport);

                // Send the status report
                requestFile = reportManager.SendEmailStatusReport(emailStatusReport);
                CurrentServiceHelper.LogeAlertFileToProcess(requestFile.RequestFileName, false, statusReportDate);

                #endregion

                #region "Send Sms Status Report"

                // reportManager.SendSmsStatusReport(requestfiles);

                #endregion

                CurrentServiceHelper.StopServiceInstanceWatch();

                connectionManager.TransferLocalReportFilesToAgency(config);

                requestFileManager.ArchiveSendReportFiles(config);
            }

            
            // End of the SendAlertService
            CurrentServiceHelper.StopBatchJobWatch();
            // End of the SendAlertService

        }
       
    }
}
